package com.tanhua.app.controller;

import com.tanhua.app.service.UserService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/users", name = "查询用户信息")
    public ResponseEntity findUserInfoVo(Long userID, Long huanxinID, @RequestHeader("Authorization") String token) {

        if (userID != null) {
            // 1、根据userID查询
            return userService.findUserInfoVo(userID);
        } else if (huanxinID != null) {
            // 2、根据huanxinID查询
            return userService.findUserInfoVo(huanxinID);
        } else {
            // 3、根据token查询
            User user = userService.findUserByToken(token);
            return userService.findUserInfoVo(user.getId());

        }

    }

    @PostMapping(value = "/users/header", name = "修改用户头像")
    public ResponseEntity updatePic(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws Exception {
        return userService.loginReginfoPic(headPhoto, token);
    }

    @PutMapping(value = "/users", name = "修改用户信息")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        return userService.updateUserInfo(userInfo, token);
    }

    @GetMapping(value = "/users/counts", name = "我的喜欢统计")
    public ResponseEntity findCounts() {
        return userService.findCounts();
    }

    @GetMapping("/users/friends/{type}")
    public ResponseEntity findType(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
            @PathVariable Integer type) {

        return userService.findType(pageNum, pageSize, type);
    }

    @PostMapping(value = "/users/fans/{likeUserId}", name = "粉丝-喜欢")
    public ResponseEntity fansLike(@PathVariable Long likeUserId) {
        return userService.fansLike(likeUserId);
    }

    @DeleteMapping("/users/like/{uid}")
    public ResponseEntity fansDisLike(@PathVariable Long uid) {
        return userService.fansDisLike(uid);
    }
}
